import axios from 'axios'

// 创建axios实例
const request = axios.create({
  baseURL: '/api', // 使用代理路径，实际地址在nuxt.config.ts中配置
  timeout: 50000, // 请求超时时间
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  }
})

// 封装GET请求
export async function get<T>(url: string, params?: any): Promise<T> {
  return request.get(url, { params }).then(res => res.data)
}

// 封装POST请求
export async function post<T>(url: string, data?: any): Promise<T> {
  return request.post(url, data).then(res => res.data)
}

// 封装PUT请求
export async function put<T>(url: string, data?: any): Promise<T> {
  return request.put(url, data).then(res => res.data)
}

// 封装DELETE请求
export async function del<T>(url: string, params?: any): Promise<T> {
  return request.delete(url, { params }).then(res => res.data)
}

// 封装PATCH请求
export async function patch<T>(url: string, data?: any): Promise<T> {
  return request.patch(url, data).then(res => res.data)
}

// 封装HEAD请求
export async function head<T>(url: string, params?: any): Promise<T> {
  return request.head(url, { params }).then(res => res.data)
}

// 封装OPTIONS请求
export async function options<T>(url: string, params?: any): Promise<T> {
  return request.options(url, { params }).then(res => res.data)
}

export default request